package com.csist.classone.csistamis;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.csist.classone.csistamis.csistamis.utils.PreUtils;

import java.util.ArrayList;

/*
快捷键：Alt+Enter :快速提示
ctrl+alt+F:提取变量为全局变量
Ctrl+Alt+M:析出方法
 */
public class GuideActivity extends AppCompatActivity {


    private ViewPager mViewPager;
    private LinearLayout mLl_dots;
    private int[] mImageResId;
    private ArrayList<ImageView> mImageViews;
    private com.csist.classone.csistamis.csistamis.utils.PreUtils PreUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guild);
        //更新sp中的is_first_enter,置为false，确保下次直接进入登陆界面
        PreUtils.setBoolean(getApplicationContext(),"is_first_enter",false);

        //初始化视图  view
        initView();
        //model 数据
        initData();
        //contrllor 控制器
        initAdapter();
    }
    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mLl_dots = (LinearLayout) findViewById(R.id.ll_dots);
    }

    private void initData() {
        //创建图片资源id的数组
        mImageResId = new int[]{R.drawable.a,R.drawable.b,
                R.drawable.c,R.drawable.d,R.drawable.e};
        //将图片转化成imageview对象
        mImageViews = new ArrayList<>();
        ImageView imageView;
        for (int i = 0; i < mImageResId.length; i++) {
            imageView = new ImageView(this);
            imageView.setBackgroundResource(mImageResId[i]);
            mImageViews.add(imageView);
        }

    }

    private void initAdapter() {
            mViewPager.setAdapter(new MyAdapter());
    }


    private class MyAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mImageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            ImageView imageView = mImageViews.get(position);
            container.addView(imageView);

            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
